package com.example.springredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ResponseBody
    @GetMapping("/testString")
    public String testString() {
        redisTemplate.opsForValue().set("key", "123");
        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value: " + value);
        return "OK " + value;
    }

    @GetMapping("/testList")
    @ResponseBody
    public String testList() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            //execute要求回调方法中必须写return语句
            //这个回调返回的对象, 就会作为 execute 本身的返回值
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "333");
        redisTemplate.opsForList().rightPush("key", "222");
        redisTemplate.opsForList().rightPush("key", "444");

        System.out.println(redisTemplate.opsForList().leftPop("key"));
        System.out.println(redisTemplate.opsForList().leftPop("key"));
        System.out.println(redisTemplate.opsForList().leftPop("key"));
        System.out.println(redisTemplate.opsForList().leftPop("key"));

        return "OK";
    }

    @GetMapping("/testSet")
    @ResponseBody
    public String testSet() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            //execute要求回调方法中必须写return语句
            //这个回调返回的对象, 就会作为 execute 本身的返回值
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println(result);
        System.out.println(redisTemplate.opsForSet().isMember("key", "111"));
        System.out.println(redisTemplate.opsForSet().size("key"));
        redisTemplate.opsForSet().remove("key", "222");

        System.out.println(redisTemplate.opsForSet().members("key"));

        return "OK";
    }

    @GetMapping("/testHash")
    @ResponseBody
    public String testHash() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            //execute要求回调方法中必须写return语句
            //这个回调返回的对象, 就会作为 execute 本身的返回值
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "222");
        redisTemplate.opsForHash().put("key", "f3", "333");

        String val = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println(val);

        Boolean res = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println(res);

        System.out.println(redisTemplate.opsForHash().size("key"));

        return "OK";
    }

    @GetMapping("/testZSet")
    @ResponseBody
    public String testZSet() {
        //先清除之前的数据
        redisTemplate.execute((RedisConnection connection) -> {
            //execute要求回调方法中必须写return语句
            //这个回调返回的对象, 就会作为 execute 本身的返回值
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key","zhangsan", 10);
        redisTemplate.opsForZSet().add("key","lisi", 20);
        redisTemplate.opsForZSet().add("key","wangwu", 30);
        Set<ZSetOperations.TypedTuple<String>> members = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println(members);

        return "OK";
    }

}
